package com.xuxueli.applyModules.utils.testcharts;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Slf4j
public class FileUtil {

    public static void main(String[] args) {
        String path = "E:";
        String name = "test.txt";
//		String result = readFileStr("E:/data/keystore/test/acp_prod_enc.cer");
//        removeFile("E:/data/6test.cer");
        try (ByteArrayInputStream in = new ByteArrayInputStream("sdsaiu傻大".getBytes(StandardCharsets.UTF_8))) {
            saveFile(path, name, in);
        } catch (Exception e) {

        }
    }

    public static void saveFile(String path, String fileName, InputStream in) {
        File pathFile = new File(path);
        if (!pathFile.exists()) {
            pathFile.mkdirs();
        }
        try (OutputStream out = new FileOutputStream(
                new File(pathFile.getAbsolutePath() + File.separator + fileName))) {
            byte[] bytes = new byte[1024];
            int length;
            while ((length = in.read(bytes)) != -1) {
                out.write(bytes, 0, length);
            }
            out.flush();
        } catch (Exception e) {
            log.error("文件保存异常", e);
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static byte[] readFileByte(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            return null;
        }
        try (FileInputStream in = new FileInputStream(file);) {
            int length = in.available();
            byte[] bytes = new byte[length];
            in.read(bytes, 0, length);
            return bytes;
        } catch (Exception e) {
            log.error("读取文件异常", e);
            return null;
        }
    }

    public static String readFileStr(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            return null;
        }
        try (BufferedReader in = new BufferedReader(new FileReader(file));) {
            List<String> lines = in.lines().collect(Collectors.toList());
            if (lines.size() == 1) {
                return lines.get(0);
            }
            String firstLine = lines.get(0);
            String lastLine = lines.get(lines.size() - 1);
            int startIndex = 0;
            int endIndex = lines.size() - 1;
            if (firstLine.startsWith("-----") && firstLine.endsWith("-----") && lastLine.startsWith("-----") && lastLine
                    .endsWith("-----")) {
                startIndex = 1;
                endIndex = lines.size() - 2;
            }
            StringBuilder stringBuffer = new StringBuilder();
            for (int index = startIndex; index <= endIndex; index++) {
                stringBuffer.append(lines.get(index));
            }
            return stringBuffer.toString().replace("\n", "");
        } catch (Exception e) {
            log.error("读取文件异常", e);
            return null;
        }
    }

    public static void removeFile(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        }
    }

    public static String randomFileName(String fileName) {
        if (StringUtils.isEmpty(fileName)) {
            return null;
        }
        String suffix = "";
        if (fileName.lastIndexOf(".") != -1) {
            suffix = fileName.substring(fileName.lastIndexOf("."));
        }
        String uuid = UUID.randomUUID().toString().replace("-", "");
        return uuid + suffix;
    }

    public static String readFile(MultipartFile file) {
        try {
            InputStreamReader read = new InputStreamReader(file.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt = bufferedReader.readLine();
            while (lineTxt != null) {
                return lineTxt;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void downloadFile(byte[] message, OutputStream out) {
        if (message == null || message.length == 0) {
            return;
        }
        try (ByteArrayInputStream in = new ByteArrayInputStream(message)) {
            byte[] bytes = new byte[2048];
            int length;
            while ((length = in.read(bytes)) != -1) {
                out.write(bytes, 0, length);
            }
            out.flush();
            out.close();
        } catch (IOException e) {
            log.error("下载文件异常", e);
        }
    }

}